package com.iplant.ams.serviceImpl.utils;

import com.iplant.ams.service.basicenum.dms.scada.DMSDeviceStatusEnum;

import java.util.*;

public class DMSDeviceStatusEnumTool {

    public List<Integer> Values = new ArrayList<>();

    public Map<String, Integer> ValuesDic = new HashMap<String, Integer>();

    public int FullValue = 0;

    private DMSDeviceStatusEnumTool() {
        Values.clear();
        ValuesDic.clear();
        for (DMSDeviceStatusEnum wDMSDeviceStatusEnum : DMSDeviceStatusEnum.values()) {
            if (wDMSDeviceStatusEnum.getValue() <= 0)
                continue;
            Values.add((int) wDMSDeviceStatusEnum.getValue());
            ValuesDic.put(wDMSDeviceStatusEnum.toString(), wDMSDeviceStatusEnum.getValue());
            FullValue |= wDMSDeviceStatusEnum.getValue();
        }
    }

    private static DMSDeviceStatusEnumTool _Instance = new DMSDeviceStatusEnumTool();

    public static DMSDeviceStatusEnumTool getInstance() {
        return _Instance;
    }


    public Map<String, List<Integer>> CompareStatus(int wOldStatus, int wNewStatus) {
        Map<String, List<Integer>> wResultMap = new HashMap<>();

        List<Integer> wResult = new ArrayList<>();
        List<Integer> wLive = new ArrayList<>();
        List<Integer> wOver = new ArrayList<>();

        wResultMap.put("Result", wResult);
        wResultMap.put("Live", wLive);
        wResultMap.put("Over", wOver);


        for (int item : Values) {
            if ((item & wOldStatus & wNewStatus) > 0)
                wResult.add(item);

            if ((item & wNewStatus) > 0 && (item & wOldStatus) <= 0)
                wLive.add(item);

            if ((item & wOldStatus) > 0 && (item & wNewStatus) <= 0)
                wOver.add(item);
        }

        //判断关机状态
        if (((wNewStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) <= 0) && ((wOldStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) <= 0)) {
            wResult.add(DMSDeviceStatusEnum.TurnOff.getValue());
        }
        if (((wNewStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) <= 0) && ((wOldStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) > 0)) {
            wLive.add(DMSDeviceStatusEnum.TurnOff.getValue());
        }
        if (((wNewStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) > 0) && ((wOldStatus & (int) DMSDeviceStatusEnum.TurnOn.getValue()) <= 0)) {
            wOver.add(DMSDeviceStatusEnum.TurnOff.getValue());
        }
        return wResultMap;
    }

    public int ListToStatus(Collection<Integer> wStatusList) {
        int wResult = 0;
        if (wStatusList == null || wStatusList.size() == 0)
            return wResult;

        for (int item : wStatusList) {
            wResult = wResult | item;
        }
        return wResult;
    }

}
